/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with Foobar.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#include "ListPanel.h"
ListPanel::ListPanel(int x, int y, int width, int height) : UIPanel(x,y,width,height)
{
	selectedIndex = -1;
	connect(this, SIGNAL(keyDown(int)),  SLOT(onKeyDown(int)), Qt::DirectConnection);
}
ListPanel::~ListPanel(void)
{
}

void ListPanel::addItem( ListItem *item )
{
	//addControl(item);
	item->Parent(this);
	itemsVector.push_back(item);
	int size = itemsVector.size();
	if(size == 1)
	{
		//only item in list
		onSelectItem(size - 1);
	}
}

void ListPanel::onItemExecute( int index )
{
	emit itemExecuted(index);
}
void ListPanel::onSelectItem(int index)
{
	if(index >= itemsVector.size())
		return;
	if(selectedIndex != -1 && itemsVector[selectedIndex] != NULL)
	{
		//deselect the active item
		itemsVector[selectedIndex]->IsSelected(false);
	}
	selectedIndex = index;
	itemsVector[selectedIndex]->IsSelected(true);
	emit selectedItemChanged(index);
}
//void ListPanel::onKeyPress( int keyCode, bool isCtrlDown )
void ListPanel::onKeyDown(int keyCode)
{
	int itemsListSize = itemsVector.size();
	//if itemList == 0 or 1, return b/c no point moving up or down
	if(keyCode == VK_UP && itemsListSize > 1)
	{		
		if(selectedIndex == 0)
		{
			//the selectedItem at front of the list
			//ignore then
			return;
		}
		onSelectItem(selectedIndex - 1);		
	}
	else if(keyCode == VK_DOWN && itemsListSize > 1)
	{		
		if(selectedIndex == (itemsListSize - 1))
		{
			//the selectedItem at end of the list
			//ignore then
			return;
		}
		onSelectItem(selectedIndex + 1);		
	}
	else if(keyCode == VK_RETURN)
	{
		if(selectedIndex != -1)
		{
			onItemExecute(selectedIndex);
		}
	}
		
}

void ListPanel::draw( LPDIRECT3DDEVICE9 device )
{
	int size = itemsVector.size();
	for(int i = 0; i < size; i++)
	{
		itemsVector[i]->draw(device);
	}
}

void ListPanel::layout( QPoint offset )
{
	layoutSelf(offset);
	int curY = 2;
	int size = itemsVector.size();
	for(int i = 0; i < size; i++)
	{
		//itemsVector[i]->layout(xOffset + 1, yOffset + curY);
		//height of each item
		curY += 20;
	}
}
void ListPanel::onDeviceReset()
{
	UIPanel::onDeviceReset();
	int size = itemsVector.size();
	for(int i = 0; i < size; i++)
	{
		itemsVector[i]->onDeviceReset();
	}
}
